﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using YArchitech.LIB;

namespace YArchitech.Controls.HeaderGridView
{
	public partial class DlgHeaderTree : System.Windows.Forms.Form
	{
		public DlgHeaderTree(Dictionary<string, string> dict, DataGridViewEx mainGridView, string dataFileName)
		{
			this.InitializeComponent();
			new HYProcessTools().ShowInTaskbarChange(this);
			this.dFileName = dataFileName;
			this.mGridView = mainGridView;
			this.mDict = dict;
			foreach (KeyValuePair<string, string> keyValuePair in dict)
			{
				this.comboBox1.Items.Add(keyValuePair.Key);
			}
			this.comboBox1.SelectedIndex = 0;
		}

		private string NewNodeName()
		{
			string str = "新节点";
			int num = 0;
			this.MaxNodeNumber(this.treeView1.Nodes, ref num);
			return str + (num + 1).ToString();
		}

		private void MaxNodeNumber(TreeNodeCollection tnc, ref int number)
		{
			foreach (object obj in tnc)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Text.IndexOf("新节点") == 0)
				{
					string value = treeNode.Text.Substring(3, treeNode.Text.Length - 3);
					try
					{
						int num = Convert.ToInt32(value);
						if (num > number)
						{
							number = num;
						}
					}
					catch (Exception)
					{
					}
				}
				this.MaxNodeNumber(treeNode.Nodes, ref number);
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			TreeNode treeNode = new TreeNode(this.NewNodeName());
			this.treeView1.Nodes.Add(treeNode);
			this.treeView1.SelectedNode = treeNode;
			this.treeView1.ExpandAll();
			this.View();
		}

		private void button4_Click(object sender, EventArgs e)
		{
			this.View();
			this.mGridView.HeadSource.Clear();
			this.mGridView.DataSource = null;
			foreach (object obj in this.treeView1.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				this.mGridView.HeadSource.Add((TreeNode)treeNode.Clone());
			}
			try
			{
				new FileInfo(this.dFileName).Delete();
				HeaderUtils.CreateHeaderXml(this.dFileName);
				HeaderUtils.CopyTree(this.dataGridViewEx1.HeadSource, this.headerTree);
				HeaderUtils.WriteToXml(this.headerTree, this.dFileName);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			base.Close();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (this.treeView1.SelectedNode != null)
			{
				this.treeView1.SelectedNode.Remove();
			}
			this.View();
		}

		private void button3_Click(object sender, EventArgs e)
		{
			if (this.treeView1.SelectedNode != null)
			{
				this.mEditBeginingNodeName = this.treeView1.SelectedNode.Text;
				this.treeView1.SelectedNode.BeginEdit();
			}
		}

		private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (this.treeView1.SelectedNode != null && this.treeView1.SelectedNode.Tag != null)
			{
				this.comboBox1.SelectedItem = this.treeView1.SelectedNode.Tag.ToString();
				return;
			}
			this.comboBox1.SelectedIndex = 0;
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.treeView1.SelectedNode != null)
			{
				this.treeView1.SelectedNode.Tag = this.comboBox1.SelectedItem;
			}
		}

		private void button7_Click(object sender, EventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.InitialDirectory = this.DataPath;
			saveFileDialog.Filter = "表头文件(*.xml)|*.xml";
			saveFileDialog.RestoreDirectory = true;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				string fileName = saveFileDialog.FileName;
				HeaderUtils.CreateHeaderXml(fileName);
				HeaderUtils.WriteToXml(this.treeView1, fileName);
			}
		}

		public string DataPath
		{
			get
			{
				int num = this.location.LastIndexOf("\\");
				string text = this.location.Substring(0, num - 1);
				int num2 = text.LastIndexOf("\\");
				return text.Substring(0, num2 - 1) + "\\Data";
			}
		}

		private void button6_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.InitialDirectory = this.DataPath;
			openFileDialog.Filter = "表头文件(*.xml)|*.xml";
			openFileDialog.RestoreDirectory = true;
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				string fileName = openFileDialog.FileName;
				if (this.TestXml(fileName))
				{
					this.treeView1.Nodes.Clear();
					HeaderUtils.LoadXml(fileName, this.treeView1);
				}
				else
				{
					MessageBox.Show("表头文件格式错误");
				}
			}
			this.View();
		}

		private bool TestXml(string fileName)
		{
			bool result;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				if (xmlDocument.ChildNodes[1].Name == "Header")
				{
					result = true;
				}
				else
				{
					result = false;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private void button8_Click(object sender, EventArgs e)
		{
			this.View();
		}

		private void View()
		{
			this.dataGridViewEx1.HeadSource.Clear();
			this.dataGridViewEx1.DataSource = null;
			foreach (object obj in this.treeView1.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				this.dataGridViewEx1.HeadSource.Add((TreeNode)treeNode.Clone());
			}
			List<string> list = new List<string>();
			list.Add("");
			this.dataGridViewEx1.DataSource = HeaderUtils.ToDataTable<string>(list, HeaderUtils.GetChilds(this.treeView1.Nodes), this.mDict);
			this.dataGridViewEx1.Refresh();
		}

		private void button9_Click(object sender, EventArgs e)
		{
			if (this.treeView1.SelectedNode != null)
			{
				TreeNode treeNode = new TreeNode(this.NewNodeName());
				this.treeView1.SelectedNode.Nodes.Add(treeNode);
				this.treeView1.SelectedNode = treeNode;
				this.treeView1.ExpandAll();
				this.View();
			}
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			this.treeView1.SelectedNode.Tag = "hide";
		}

		private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
		{
			e.DrawDefault = true;
			if ((e.State & TreeNodeStates.Selected) != (TreeNodeStates)0)
			{
				e.Graphics.FillRectangle(Brushes.DarkBlue, e.Node.Bounds);
				Font font = e.Node.NodeFont;
				if (font == null)
				{
					font = ((TreeView)sender).Font;
				}
				e.Graphics.DrawString(e.Node.Text, font, Brushes.White, Rectangle.Inflate(e.Bounds, 2, 0));
			}
			else
			{
				e.DrawDefault = true;
			}
			if ((e.State & TreeNodeStates.Focused) != (TreeNodeStates)0)
			{
				using (Pen pen = new Pen(Color.Black))
				{
					pen.DashStyle = DashStyle.Dot;
					Rectangle bounds = e.Node.Bounds;
					bounds.Size = new Size(bounds.Width - 1, bounds.Height - 1);
					e.Graphics.DrawRectangle(pen, bounds);
				}
			}
		}

		private void cbxDefault_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cbxDefault.SelectedItem != null)
			{
				string fullName = ((HeaderFile)this.cbxDefault.SelectedItem).FileInfo.FullName;
				if (this.TestXml(fullName))
				{
					this.treeView1.Nodes.Clear();
					HeaderUtils.LoadXml(fullName, this.treeView1);
					this.View();
					return;
				}
				MessageBox.Show("表头文件格式错误");
			}
		}

		private bool IsValidXmlItemName(string text, ref string errMessage)
		{
			if (text.CompareTo("") == 0)
			{
				errMessage = "列名不能为空。";
				return false;
			}
			if (text.IndexOf(" ") >= 0)
			{
				errMessage = "列名不能包含空格。";
				return false;
			}
			if (text.ToUpper().IndexOf("XML") == 0)
			{
				errMessage = "列名不能以XML开头。";
				return false;
			}
			if (char.IsPunctuation(text[0]))
			{
				errMessage = "列名不能以标点符号开头。";
				return false;
			}
			if (char.IsNumber(text[0]))
			{
				errMessage = "列名不能以数字开头。";
				return false;
			}
			if (char.IsSeparator(text[0]))
			{
				errMessage = "列名不能以分隔符开头。";
				return false;
			}
			return true;
		}

		private void getColumnNames(TreeNodeCollection nodes, ref List<string> names)
		{
			foreach (object obj in nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				names.Add(treeNode.Text.Trim());
				if (treeNode.Nodes.Count > 0)
				{
					this.getColumnNames(treeNode.Nodes, ref names);
				}
			}
		}

		private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
		{
			TreeView treeView = sender as TreeView;
			if (treeView.SelectedNode != null)
			{
				if (e.Label == null)
				{
					e.CancelEdit = true;
					return;
				}
				if (e.Label.CompareTo("") == 0)
				{
					treeView.SelectedNode.EndEdit(true);
					e.CancelEdit = true;
					MessageBox.Show("列名为空。");
					return;
				}
				List<string> list = new List<string>();
				this.getColumnNames(treeView.Nodes, ref list);
				if (list.Contains(e.Label.Trim()))
				{
					treeView.SelectedNode.EndEdit(true);
					e.CancelEdit = true;
					MessageBox.Show("列名重复。");
					return;
				}
				string text = "";
				if (!this.IsValidXmlItemName(e.Label, ref text))
				{
					treeView.SelectedNode.EndEdit(true);
					treeView.SelectedNode.Text = this.mEditBeginingNodeName;
					treeView.SelectedNode.Name = this.mEditBeginingNodeName;
					e.CancelEdit = true;
					MessageBox.Show(text);
				}
				treeView.SelectedNode.Name = e.Label;
			}
		}

		private void treeView1_Leave(object sender, EventArgs e)
		{
			this.View();
		}

		private void treeView1_Click(object sender, EventArgs e)
		{
			this.View();
		}

		private DataGridViewEx mGridView;

		private Dictionary<string, string> mDict;

		private string dFileName = "";

		private string mEditBeginingNodeName = "";

		private TreeView headerTree = new TreeView();

		private string location = Assembly.GetExecutingAssembly().Location;
	}
}
